package hu.myandroidlife.activity;

import hu.myandroidlife.R;
import hu.myandroidlife.R.anim;
import hu.myandroidlife.R.id;
import hu.myandroidlife.R.layout;
import hu.myandroidlife.base.BaseActivity;
import hu.myandroidlife.myview.wheelview.AbstractWheel;
import hu.myandroidlife.myview.wheelview.OnWheelScrollListener;
import hu.myandroidlife.myview.wheelview.adapter.NumericWheelAdapter;
import hu.myandroidlife.util.WeiboDialogUtils;

import java.util.Calendar;

import android.content.Intent;
import android.graphics.Color;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class IosDateSelectActivity extends BaseActivity {

	private LayoutInflater inflater = null;
	private LinearLayout ll;
	private int mYear = 1950;
	private int mMonth = 0;
	private int mDay = 1;
	private View view;
	private AbstractWheel year;
	private AbstractWheel month;
	private TextView tvSure;
	private String n_day;
	private String n_month;
	private String n_year;

	private int mYear2;
	private int mMonth2;

	private Button btnDialogExitBottom;
	private TextView tvShowDateTime;
	private View mShowDialogLayout;
	private View mCancelDialogLayout;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_ios_date_select2;
	}

	@Override
	protected void initView() {
		btnDialogExitBottom = $findViewById(R.id.btnDialogExitBottom);
		tvShowDateTime = $findViewById(R.id.tvShowDateTime);
		mShowDialogLayout = $findViewById(R.id.dialog_show_layout);
		mCancelDialogLayout = $findViewById(R.id.cancel_dialog_layout);

		inflater = (LayoutInflater) this
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		ll = $findViewById(R.id.ll);
		ll.addView(getDataPick());

		tvSure = $findViewById(R.id.tvSure);
		tvSure.setOnClickListener(this);
	}

	@Override
	protected void initListener() {
		btnDialogExitBottom.setOnClickListener(this);
	}

	@Override
	protected void widgetClick(View v) {
		switch (v.getId()) {
		case R.id.btnDialogExitBottom:
			showCancelDialogState(true);
			break;
		case R.id.tvSure:
			// if(n_year==null){
			// n_year=1970+"";
			// n_month=1+"";
			// }
			// tvShowDateTime.setText(String.format("%02d",
			// Integer.parseInt(n_year))+"-"+String.format("%02d",
			// Integer.parseInt(n_month)));

			if (mYear2 == 0) {
				mYear2 = 1970;
				mMonth2 = 1;
			}
			tvShowDateTime.setText(String.format("%02d", mYear2) + "-"
					+ String.format("%02d", mMonth2));
			showCancelDialogState(false);
			break;
		default:
			break;
		}
	}

	private void showCancelDialogState(boolean isVisable) {
		Animation anim = null;
		if (isVisable) {
			anim = AnimationUtils
					.loadAnimation(this, R.anim.slide_bottom_to_up);
			mCancelDialogLayout.setAnimation(anim);
			mShowDialogLayout.setVisibility(View.VISIBLE);

		} else {
			anim = AnimationUtils
					.loadAnimation(this, R.anim.slide_up_to_bottom);
			mCancelDialogLayout.setAnimation(anim);
			mShowDialogLayout.setVisibility(View.GONE);
		}
	}

	private View getDataPick() {
		Calendar c = Calendar.getInstance();
		int norYear = c.get(Calendar.YEAR);
		int curYear = mYear;
		int curMonth = mMonth + 1;

		view = inflater.inflate(R.layout.resgiter_wheel_date_picker, null);
		year = (AbstractWheel) view.findViewById(R.id.year);
		NumericWheelAdapter numericWheelAdapter1 = new NumericWheelAdapter(
				this, 1930, norYear - 1);
		numericWheelAdapter1.setTextSize(26);
		numericWheelAdapter1.setTextColor(Color.rgb(106, 108, 108));
		year.setViewAdapter(numericWheelAdapter1);
		// year.setCyclic(true);//是否可循环滑动
		year.addScrollingListener(scrollListener);

		month = (AbstractWheel) view.findViewById(R.id.month);
		NumericWheelAdapter numericWheelAdapter2 = new NumericWheelAdapter(
				this, 1, 12, "%02d");
		numericWheelAdapter2.setTextSize(26);
		numericWheelAdapter2.setTextColor(Color.rgb(106, 108, 108));
		month.setViewAdapter(numericWheelAdapter2);
		month.setCyclic(true);
		month.addScrollingListener(scrollListener);

		initDay(curYear, curMonth);

		year.setCurrentItem(40);

		year.setVisibleItems(3);
		month.setVisibleItems(3);
		return view;
	}

	OnWheelScrollListener scrollListener = new OnWheelScrollListener() {

		@Override
		public void onScrollingStarted(AbstractWheel wheel) {
		}

		@Override
		public void onScrollingFinished(AbstractWheel wheel) {

			mYear2 = year.getCurrentItem() + 1930;// 年
			mMonth2 = month.getCurrentItem() + 1;// 月

			n_year = year.getCurrentItem() + 1930 + "";// 年
			n_month = month.getCurrentItem() + 1 + "";// 月

		}
	};

	private int getDay(int year, int month) {
		int day = 30;
		boolean flag = false;
		switch (year % 4) {
		case 0:
			flag = true;
			break;
		default:
			flag = false;
			break;
		}
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			day = 31;
			break;
		case 2:
			day = flag ? 29 : 28;
			break;
		default:
			day = 30;
			break;
		}
		return day;
	}

	private void initDay(int arg1, int arg2) {
		NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this,
				1, getDay(arg1, arg2), "%02d");
		numericWheelAdapter.setTextSize(26);
		numericWheelAdapter.setTextColor(Color.rgb(106, 108, 108));
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			if(mShowDialogLayout.getVisibility()==View.VISIBLE){
				showCancelDialogState(false);
			}else{
				finish();
			}
			return true;
		default:
			break;
		}
		return true;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction()==MotionEvent.ACTION_DOWN){
			if(mShowDialogLayout.getVisibility()==View.VISIBLE){
				showCancelDialogState(false);
			}
			return true;
		}
		return super.onTouchEvent(event);
	}

}
